using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core;
using Forest.Core.Caching;
using Forest.Core.Data;
using Forest.Core.Events;

namespace Forest.Cat.Domain.Services.Pictures
{
    public partial class PictureService:IPictureService
    {
        #region Constants
        private const string PICTURE_BY_ID_KEY = "picture.id-{0}";
        private const string PICTURE_PATTERN_KEY = "picture.";
        #endregion

        #region Fields
        private readonly IRepository<Picture> _pictureRepository;
		private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public PictureService(IRepository<Picture> pictureRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _pictureRepository = pictureRepository;
			_cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeletePicture(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("Picture");

            _pictureRepository.Delete(picture);
			_cacheManager.RemoveByPattern(PICTURE_PATTERN_KEY);
			_eventPublisher.EntityDeleted(picture);
        }

        public Picture GetPictureById(Guid pictureId)
        {
            if (pictureId == Guid.Empty)
                return null;
            string key = string.Format(PICTURE_BY_ID_KEY, pictureId);

            return _cacheManager.Get(key, () => {
                var cl = _pictureRepository.GetById(pictureId);
                return cl;
            });
        }

        public virtual IPagedList<Picture> GetAllPictures(int pageIndex, int pageSize)
        {
            var query = _pictureRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Picture>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertPicture(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("Picture");
            _pictureRepository.Insert(picture);
			_cacheManager.RemoveByPattern(PICTURE_PATTERN_KEY);
			_eventPublisher.EntityInserted(picture);
        }

        public virtual void UpdatePicture(Picture picture)
        {
            if (picture == null)
                throw new ArgumentNullException("Picture");
            _pictureRepository.Update(picture);
			_cacheManager.RemoveByPattern(PICTURE_PATTERN_KEY);
			_eventPublisher.EntityUpdated(picture);
        }
        #endregion

        #region Extend Methods

        public IList<Guid> GetCompanyPicturesId(Guid companyId, bool isLogo)
        {
            return _pictureRepository.Table.Where(x => x.CompanyId.Equals(companyId) && x.IsLogo == isLogo).Select(x=>x.Id).ToList();
        }

        #endregion
    }
}
